package com.botao.cms.controller;

import com.botao.cms.pojo.User;
import com.botao.cms.service.CommonService;
import com.botao.cms.service.UserService;
import com.botao.cms.utils.AjaxResult;
import com.botao.cms.utils.RequestUtils;
import com.botao.cms.utils.VerifyCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

@Slf4j
@Controller
public class BaseController {
    @Resource
    CommonService commonService;
    @Autowired
    UserService userService;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/")
    public String index(){
        return "redirect:login";
    }
    @GetMapping(path = {"login"})
    public String login() {
        return "login";
    }

    @GetMapping("logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:login";
    }

    @GetMapping(path = {"admin", "admin.html"})
    public String admin() {
        return "admin/admin";
    }

    @GetMapping(path = {"admin/home", "admin/home.html"})
    public String home() {
        return "admin/home";
    }

    @ResponseBody
    @PostMapping("register")
    public AjaxResult register(String email, String password, String yzm, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String registerYzm = (String) session.getAttribute("registerYzm");
        if (!registerYzm.equals(yzm)) {
            return AjaxResult.error(700, "验证码错误");
        }
        if (userService.register(email, password)) {
            return AjaxResult.success("注册成功");
        } else {
            return AjaxResult.error(499, "注册失败,该邮箱已被注册！");
        }

    }

    @ResponseBody
    @PostMapping("login/check")
    public AjaxResult loginCheck(String email, String password, String yzm, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String verifyCode = (String) session.getAttribute("verifyCode");
        if (!verifyCode.toLowerCase().equals(yzm.toLowerCase())) {
            return AjaxResult.error(700, "验证码错误");
        }
        User user = userService.login(email, password);
        if (user == null) {
            return AjaxResult.error("密码错误");
        } else {
            session.setAttribute("user", user);
            return AjaxResult.success(user);
        }

    }


    @ResponseBody
    @PostMapping("register/yzm")
    public void registerYzm(String email, HttpServletRequest request) {
        commonService.sendMailCheck(request.getSession(), email);
    }


    /**
     * 获取验证码图片
     */
    @RequestMapping("login/getVerifyCode")
    public void getVerificationCode(HttpServletResponse response, HttpServletRequest request) {
        try {
            int width = 200;
            int height = 69;
            BufferedImage verifyImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            String randomText = VerifyCode.drawRandomText(width, height, verifyImg);
            request.getSession().setAttribute("verifyCode", randomText);
            response.setContentType("image/png");
            OutputStream os = response.getOutputStream();
            ImageIO.write(verifyImg, "png", os);
            os.flush();
            os.close();//关闭流
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

